package com.hyx.bookManager.controller;

import com.hyx.bookManager.domain.common.CommonResult;
import com.hyx.bookManager.service.BookService;
import com.hyx.bookManager.utils.TokenUtil;
import com.nimbusds.jose.JOSEException;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * @ClassName BookController
 * @Description TODO
 * @Author 黄乙轩
 * @Date 2021/6/7 22:28
 * @Version 1.0
 **/

@CrossOrigin
@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @Autowired
    private TokenUtil tokenUtil;

    @ResponseBody
    @RequestMapping("/borrowingBook")
    public CommonResult borrowingBook(@RequestBody Map<String, String> map, HttpServletRequest request) throws ParseException, JOSEException {
        String token = request.getHeader("token");
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime after = now.plusDays(Long.valueOf(map.get("day")));
        Long bookId = Long.valueOf(map.get("bookId"));
        Map<String, Object> tokenMap = tokenUtil.validHS256(token);
        JSONObject data = (JSONObject) tokenMap.get("data");
        String id = (String) data.get("id");
        return bookService.borrowingBook(now, after, bookId, id);
    }

    @ResponseBody
    @RequestMapping("/getBookByTagId")
    public CommonResult getBookByTagId(@RequestBody Map<String, String> map) {
        Integer page = Integer.valueOf(map.get("page"));
        Integer pageSize = Integer.valueOf(map.get("pageSize"));
        Long tagId;
        if (map.get("id") == null) {
            tagId = null;
        } else {
            tagId = Long.valueOf(map.get("id"));
        }
        return bookService.getBookByTagId(page, pageSize, tagId);
    }

    @ResponseBody
    @RequestMapping("/getBookCount")
    public CommonResult getBookCount(@RequestBody Map<String, String> map) {
        Integer pageSize = Integer.valueOf(map.get("pageSize"));
        String tagId = map.get("tagId");
        String status = map.get("status");
        return bookService.getBookCount(pageSize, tagId, status);
    }

    @ResponseBody
    @RequestMapping("/getMyBorrowingBook")
    public CommonResult getMyBorrowingBook(@RequestBody Map<String, String> map,HttpServletRequest request) throws ParseException, JOSEException {
        String token = request.getHeader("token");
        Map<String, Object> tokenMap = tokenUtil.validHS256(token);
        JSONObject data = (JSONObject) tokenMap.get("data");
        String id = (String) data.get("id");
        String page = map.get("page");
        String pageSize = map.get("pageSize");
        return bookService.getMyBorrowingBook(id,page,pageSize);
    }

    @ResponseBody
    @RequestMapping("/getBookTotalPage")
    public CommonResult getBookTotalPage(@RequestBody Map<String,String> map){
        Long pageSize = Long.valueOf(map.get("pageSize"));
        String tagId = map.get("tagId");
        return bookService.getBookTotalPage(pageSize,tagId);
    }
}
